package com.audaque.springboot.foshanupload.jwtcore.component;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SignUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;


import com.audaque.springboot.foshanupload.jwtcore.properties.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zgb
 * @desc ...
 * @date 2022-11-23 16:46:11
 */
@Component
public class HutoolSecureUtilExtProvider extends SecureUtil {
    /**
     * Java中变量初始化顺序为：
     * 静态变量或静态语句块(按声明顺序)–> 非静态变量或构造代码块(按声明顺序)–> 构造方法 –> @Value/@Autowired等
     * 如果想要为静态成员变量自动赋值，可以使用@Autowired作用在set方法上来进行注入
     */
    //静态方法可以给new时候使用而不需要关注bean的顺序
    private static String pem_pkcs1_prv;

    private static String pem_pkcs1_pub;


    private  String skey;


    @Autowired
    public void setPem_pkcs1_prv(JwtProperties jwtPropertiese) {
        this.pem_pkcs1_prv = jwtPropertiese.getPrivateKey();
    }

    @Autowired
    public void setPem_pkcs1_pub(JwtProperties jwtPropertiese) {
        this.pem_pkcs1_pub = jwtPropertiese.getPublicKey();
    }

    @Autowired
    public void setSkey(JwtProperties jwtPropertiese) {
        this.skey = jwtPropertiese.getSkey();
    }


    public static Sign sign(SignAlgorithm algorithm) {
        Sign sign = SignUtil.sign(algorithm, pem_pkcs1_prv, pem_pkcs1_pub);
        return sign;
    }

}
